import { createStore } from 'vuex'
import { orginfo, perinfo, logout } from '~/api/manager'
import { setStorage, getStorage, removeStorage } from '~/composables/storage'
import { toast } from '~/composables/util'
import { router } from "~/router"
const store = createStore({
    state() {
        return {

        }
    },
    mutations: {
        // 记录用户信息
        SET_USERINFO(state, user) {
            state.user = user
        },
        SET_USERTYPE(state, userType) {
            state.userType = userType
        },
        SET_USERMESSAGE(state, userMessage) {
            state.userMessage = userMessage
        },
        SET_BOTTOM(state, Flag) {
            state.isBottom = Flag
        },
        SET_PAGE(state, Flag) {
            state.isShowPage = Flag
        }
    },
    getters: {
        userID: (state) => {
            if (!state.userType) return null
            if (state.userType == 1) {
                return state.userMessage.orgId
            } else {
                return state.userMessage.userId
            }
        }
    },
    actions: {
        // 获取当前登录用户信息
        login({ commit, state }) {
            return new Promise((resolve, reject) => {
                
               
            })
        },
        // 退出登录
        logout({ state, dispatch }) {
            logout(state.user).then(res => {
                if (res && res.code == '0000') {
                    toast('退出成功')
                    dispatch('clearFn')
                    router.push('/login')
                }
            })
        },
        clearFn({ commit }) {
            commit("SET_USERINFO", {})
            commit("SET_USERTYPE", null)
            commit("SET_USERMESSAGE", null)
            removeStorage('userInfo')
            removeStorage('userType')
            removeStorage('userMessage')
            removeStorage('token')
        },
        init({ commit }) {
            let userInfo = getStorage('userInfo')
            let userType = getStorage('userType')
            let userMessage = getStorage('userMessage')
            if (userInfo) {
                commit("SET_USERINFO", userInfo)
            }
            if (userType) {
                commit("SET_USERTYPE", userType)
            }
            if (userMessage) {
                commit("SET_USERMESSAGE", userMessage)
            }
        }
    }
})

export default store